////////////////////////////////////////////////////////////////////////////////
// 关闭弹出选择窗口

function closePopupWnd() {
    var index = parent.arrDialog.length - 1;
    if (index >= 0) {
        var $dialog = parent.arrDialog[index];
        $dialog.find('.dialog__close_icon').click();
    }
}

////////////////////////////////////////////////////////////////////////////////
// 打印预览
function PrintPreview() {
    bdhtml = window.document.body.innerHTML;
    sprnstr = "<!--begin-->";
    eprnstr = "<!--end-->";
    tsprnstr = "<!--tbegin -->";
    teprnstr = "<!--tend -->";
    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr));
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
    tprnhtml = bdhtml.substr(bdhtml.indexOf(tsprnstr));
    tprnhtml = tprnhtml.substring(0, tprnhtml.indexOf(teprnstr));
    window.document.body.innerHTML = prnhtml + tprnhtml;
    window.print();
    window.document.body.innerHTML = bdhtml;
}

////////////////////////////////////////////////////////////////////////////////
// 显示和隐藏 Loading.. 信息框

function showLoadmask() {
    $(document).swLoadmask({element: 'body'});
}
function hideLoadmask() {
    //setTimeout(function () {
    $.unmaskElement($(document));
    //}, 500);
}

////////////////////////////////////////////////////////////////////////////////
// 检索框处理

function CloseFilterWnd() {
    var $filterWnd = $('.filterWnd');

    if ($filterWnd.length > 0) {
        if (isFilterWndOpen === true) {
            $(".elmFilter").show();
            $(".filterWnd").toggle("slow");
            isFilterWndOpen = false;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// 页面初始化

var transitionProp = function () {
    var n = document.createElement("fakeelement"),
            i = {transition: "transitionend", OTransition: "oTransitionEnd", MozTransition: "transitionend", WebkitTransition: "webkitTransitionEnd"};
    for (var t in i) {
        if (void 0 !== n.style[t])
            return i[t];
    }
}();

$(document).ready(function () {
    if ($(".error-msg").length > 0)
        $(".error-msg").remove();

    $(document).keydown(function (event) {
        if (event.keyCode === 13) {
            if ($(event.target).closest("textarea").length === 0)
                return false;
        }
    });
    // 用回车替代tab
    var $target = $('input,select,textarea,hidden');
    $target.bind('keyup', function (e) {
        var key = e.keyCode;
        if (key === 13) {
            var nxtIdx = $target.index(this) + 1;
            if ($target.eq(nxtIdx).attr("type") === "textarea") {
                e.preventDefault();
                $target.eq(nxtIdx).focus();
                return false;
            } else if ($target.eq(nxtIdx).attr("type") === "hidden") {
                nxtIdx = nxtIdx + 1;
                $target.eq(nxtIdx).focus();
            } else {
                $target.eq(nxtIdx).focus();
            }
        }
    });

    var $search = $('.search');
    var clazz = 'search--active';
    // 搜索按钮
    $('#search_btn').on('click', function (event) {
        $search.addClass(clazz);
        $search.on(transitionProp, function (event) {
            $search.find('input').first().focus();
        });
    });

    // 搜索窗口关闭
    $('#search__form_butclose').on('click', function (event) {
        $search.removeClass(clazz);
    });

    // 点击窗口任意位置或者搜索按钮，关闭打开的菜单以及检索框
    $(document).on('click', function (event) {
        if ($(event.target).is('.search--active')) {
            $search.removeClass(clazz);
        } else {
            if ($(event.target).is('.act__button')) {
                $search.removeClass(clazz);
            }
        }
    });

    onPageResize();
    onPageReady();
});

$(window).bind("resize", function (event) {
    onPageResize();
    //resizeContainer();
});
$(window).bind("load", function (event) {
    onPageResize();
    //resizeContainer();
});

// 设置对话框的位置、遮罩大小
function resizeContainer() {
}